package bubbleFishBob.utilities;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLReader {
    
    private File myFile;
    
    public XMLReader(File file)
    {
        myFile = file;
    }

    public List<String> readFile(String level) {

        String[] result = new String[2];
        List<String> results = new ArrayList<String>();
        
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(myFile);
            doc.getDocumentElement().normalize();
            
            NodeList nodeLst = doc.getElementsByTagName(level);

            
            for (int s = 0; s < nodeLst.getLength(); s++) {
                
                Node fstNode = nodeLst.item(s);

                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) fstNode;
                    
                    results.add(this.getTagValue("Background", eElement));
                    results.add(this.getTagValue("Class", eElement));
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return results;
    }
    
//    private NodeList getLevelTag(String level)
//    {
//        NodeList nodeLst = null;
//        try
//        {
//            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//            DocumentBuilder db = dbf.newDocumentBuilder();
//            Document doc = db.parse(myFile);
//            doc.getDocumentElement().normalize();
//
//            nodeLst = doc.getElementsByTagName(level);
//        }
//        catch (Exception e)
//        {
//            e.printStackTrace();
//        }
//
//        return nodeLst;
//    }
//    
//    public String getBackgroundPath(String level)
//    {
//        
//    }
    
    
    
    private String getTagValue(String sTag, Element eElement){
        NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0); 
     
        return nValue.getNodeValue();    
     }
    
    public static void main(String[] args)
    {
        XMLReader test = new XMLReader(new File("bin/bubbleFishBob/resources/BubbleFishBob.xml"));
        for(String str: test.readFile("Level1"))
        {
            System.out.println(str);
        }
    }
    
}
